<?php

    require_once dirname(dirname(__FILE__)) . '/config/config.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';

    class dao_affiliate_register_form
    {
        public function get_all() {
            $db = new connection();
            $con = $db->open_connect();

            $query = "SELECT f.*, u.username AS username "
                    . "FROM tbl_affiliate_register_form f "
                    . "LEFT JOIN tbl_user u ON f.user_id = u.id "
                    . "WHERE f.is_approved = 0";
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());

            $list = array();
            while ($row = mysqli_fetch_array($result)) {
                array_push($list, $row);
            }
            $db->close_connect();
            return $list;
        }
        
        public function delete($id) {
            $db = new connection();
            $con = $db->open_connect();

            $query = "DELETE FROM tbl_affiliate_register_form WHERE id = " . $id;
            mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $db->close_connect();
            return TRUE;
        }
        
        public function approve_form($id) {
            $db = new connection();
            $con = $db->open_connect();

            $query = "UPDATE tbl_affiliate_register_form SET "
                    . "is_approved = 1 "
                    . "WHERE id = " . $id;
            mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $db->close_connect();
            return TRUE;
        }
        
        
        public function submit_form($user_id, $question_1, $question_2, 
                $question_3, $question_4, $question_5)
        {
            $db  = new connection();
            $con = $db->open_connect();

            $query = "INSERT INTO tbl_affiliate_register_form(user_id, question_1, question_2, question_3,
                question_4, question_5) VALUES (
                " . $user_id . ",'" . $question_1 . "','" . $question_2 . "','" . $question_3 . "','" . $question_4 . "','" . $question_5 . "')";
            $result = mysqli_query($con, $query)
            or die("Query fail: " . mysqli_error());
            $db->close_connect();

            return TRUE;
        }
        
        public function get_by_id($id) {
            $db = new connection();
            $con = $db->open_connect();

            $query = "SELECT * FROM tbl_affiliate_register_form WHERE id = " . $id;
            $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

            $row = mysqli_fetch_array($result);

            $db->close_connect();
            return $row;
        }
    }